﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;

namespace SacMauVN.Admin
{
    public partial class TroGiup : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ThemMoiBanner.Visible = false;
                LoadListTroGiup();
            }
        }

        protected void LoadListTroGiup()
        {
            string sql = "SELECT * FROM dbo.tblTroGiup";
            DataTable dt = SqlHelper.ExecuteDataTable(CommandType.Text, sql);
            grvData.DataSource = dt;
            grvData.DataBind();

        }

        protected void btnThemMoi_Click(object sender, EventArgs e)
        {
            ThemMoiBanner.Visible = true;
        }

        protected void btnLuu_Click(object sender, EventArgs e)
        {
            ThemMoiTroGiup();
            LoadListTroGiup();
            ThemMoiBanner.Visible = false;
        }

        protected void ThemMoiTroGiup()
        {
            List<SqlParameter> arrParams = new List<SqlParameter>();
            string sqlUpload = "";
            if (TroGiupUpload.HasFile)
            {
                sqlUpload = "@AttachFile,";
                arrParams.Add(SqlHelper.CreateParameterObject("@AttachFile", SqlDbType.NVarChar, ParameterDirection.Input, UploadFile()));
            }
            string sql = "Insert into dbo.tblTroGiup values(@TieuDe,@NoiDung," + sqlUpload + "@UserID,GETDATE(),GETDATE())";
            arrParams.Add(SqlHelper.CreateParameterObject("@TieuDe", SqlDbType.NVarChar, ParameterDirection.Input, txtTieuDe.Text));
            arrParams.Add(SqlHelper.CreateParameterObject("@NoiDung", SqlDbType.NVarChar, ParameterDirection.Input, txtNoiDung.Value));
            arrParams.Add(SqlHelper.CreateParameterObject("@UserID", SqlDbType.Int, ParameterDirection.Input, Session["UserID"]));
            SqlHelper.ExecuteNonQuery(CommandType.Text, sql, arrParams);
        }

        protected void SuaTroGiup(int ID)
        {
            List<SqlParameter> arrParams = new List<SqlParameter>();
            string sqlUpload = "";
            if (TroGiupUpload.HasFile)
            {
                sqlUpload = "AttachFile = @AttachFile,";
                arrParams.Add(SqlHelper.CreateParameterObject("@AttachFile", SqlDbType.NVarChar, ParameterDirection.Input, UploadFile()));
            }
            string sql = "UPDATE tblTroGiup SET TieuDe = @TieuDe, NoiDung = @NoiDung," + sqlUpload + " UpdateDate = @UpdateDate,UserID = @UserID WHERE ID = @ID";


            arrParams.Add(SqlHelper.CreateParameterObject("@TieuDe", SqlDbType.NVarChar, ParameterDirection.Input, txtTieuDe.Text));
            arrParams.Add(SqlHelper.CreateParameterObject("@NoiDung", SqlDbType.NVarChar, ParameterDirection.Input, txtNoiDung.Value));
            arrParams.Add(SqlHelper.CreateParameterObject("@UserID", SqlDbType.Int, ParameterDirection.Input, Session["UserID"]));
            arrParams.Add(SqlHelper.CreateParameterObject("@ID", SqlDbType.Int, ParameterDirection.Input, ID));
            SqlHelper.ExecuteNonQuery(CommandType.Text, sql, arrParams);
        }

        private string UploadFile()
        {
            try
            {
                if (TroGiupUpload.HasFile)
                {
                    string sFolderPath = Server.MapPath(@"../userfile/VanBanTaiLieu");
                    HttpPostedFile myFile = TroGiupUpload.PostedFile;
                    string sFileName = myFile.FileName;
                    string[] arrFileName = sFileName.Split('.');
                    arrFileName[0] = DateTime.Now.ToString("ddMMyyhhssmm");
                    sFileName = arrFileName[0] + "." + arrFileName[1];
                    myFile.SaveAs(string.Format(@"{0}\{1}", sFolderPath, sFileName));
                    return "userfile/VanBanTaiLieu/" + sFileName;
                }
                else
                {
                    return "";
                }
            }
            catch (Exception)
            {
                return "";
            }
        }

        protected void grvData_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            string sID = e.CommandArgument.ToString().Trim();
            string sCommandName = e.CommandName;

            if ((sCommandName == SacMauVN.MyConst.Action.Approve))
            {
                List<SqlParameter> arrParams = new List<SqlParameter>();
                RadioButton rbtSuDung = (RadioButton)grvData.Rows[grvData.EditIndex].Cells[1].Controls[1];
                TextBox txtSTT = (TextBox)grvData.Rows[grvData.EditIndex].Cells[2].Controls[0];
                SuaTroGiup(Convert.ToInt32(sID));
            }
            if ((sCommandName == SacMauVN.MyConst.Action.Delete))
            {
                string sql = "Select * from tblTroGiup where ID = " + sID;
                DataTable dt = SqlHelper.ExecuteDataTable(CommandType.Text, sql);
                string FileName = dt.Rows[0]["AttachFile"].ToString();
                sql = "DELETE FROM dbo.tblTroGiup WHERE ID = " + sID;
                SqlHelper.ExecuteNonQuery(CommandType.Text, sql);
                try
                {
                    File.Delete(Server.MapPath("../" + FileName));
                }
                catch (Exception ex)
                {
                    Response.Write(ex.StackTrace);
                }


            }
            grvData.EditIndex = -1;
            LoadListTroGiup();
        }

        protected void grvData_RowEditing(object sender, GridViewEditEventArgs e)
        {
            grvData.EditIndex = e.NewEditIndex;
            LoadListTroGiup();
        }

        protected void grvData_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            grvData.EditIndex = -1;
            LoadListTroGiup();
        }

        protected void grvData_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            grvData.DeleteRow(e.RowIndex);
            grvData.DataBind();
        }

        protected void btnHuy_Click(object sender, EventArgs e)
        {
            ThemMoiBanner.Visible = false;
            txtTieuDe.Text = "";
            txtNoiDung.Value = "";
        }
    }
}
